#include <iostream>
#include <list>
#include "definitions.hpp"

using namespace std;

// Global Declarations Begin

list<unsigned char*> packet_q;
list<string> interface_list;		

// Global Declarations End

void parse_arp(){
	char buffer[4000];
	FILE *fp = fopen("/proc/net/arp","r");
	if(fp == NULL){
		cerr<<"Error in opening file! Please try again\n";
		//exit(1);
	}
        bzero(buffer,4000);
	int loop_counter = 0;
	while(fgets(buffer,4000,fp)!=NULL){
	loop_counter++;
		if(loop_counter == 1){
			continue;
		}else{
			//Parse buffer here
			int inner_loop = 0;
			char *pch;
			pch = strtok (buffer," ");
			string ipadd;
  			while (pch != NULL)
  			{
				inner_loop++;

				if(inner_loop == 1) // Parse for IPAddress, making the last 8 Bits zero
				{	ipadd.clear();
					ipadd = string(pch);
					//ipadd[ipadd.length()-1] = '0';
				}

				if(inner_loop == 4)
				{
					string macadd = string(pch);


					pch = strtok (NULL," ");
					pch = strtok (NULL," ");

					string inter = string(pch);
					inter = inter.substr(0,inter.length()-1);
					//push to interface list
					//interface_list.push_back(inter);
					string ipadd_tmp;
					ipadd_tmp.clear();
					ipadd_tmp = ipadd.substr(0,4);
					if(ipadd_tmp.compare("10.1") == 0){
                        interface_list.push_back(inter);
						//dest_mac = macadd;		
                            
					}else;

					break;
				}
			       pch = strtok (NULL, " ");
 		        }
		}

	bzero(buffer,4000);

	}

}


int main(int argc,char** argvs){

    // Begin Parsing //

	parse_arp();
	// End Parsing //


	cout<<"Creating Sniffer Thread on Client"<<endl;

  // Create Sniffer Thread Begin //
  pthread_t sniffer_thread;
            pthread_attr_t attr_sniffer;
            int thrd_sniffer=0;
            void* status_sniffer;

            pthread_attr_init(&attr_sniffer);
            pthread_attr_setdetachstate(&attr_sniffer, PTHREAD_CREATE_JOINABLE);

            thrd_sniffer=pthread_create(&sniffer_thread,&attr_sniffer,sniffer_thread_func,(void *)1);
            if(thrd_sniffer){
                cerr<<"Error; return code from thread sniffer create call is"<<thrd_sniffer<<endl;
                exit(-1);
            }

  pthread_attr_destroy(&attr_sniffer);


  // Sniffer1 Thread End          //

  	cout<<"Creating Read Write Thread on Client"<<endl;

  // Create Read Write Thread Begin //

  pthread_t rdwr_thread;
            pthread_attr_t attr_rdwr;
            int thrd_rdwr=0;
            void* status_rdwr;

            pthread_attr_init(&attr_rdwr);
            pthread_attr_setdetachstate(&attr_rdwr, PTHREAD_CREATE_JOINABLE);

            thrd_rdwr=pthread_create(&rdwr_thread,&attr_rdwr,rdwr_thread_func,(void *)1);
            if(thrd_rdwr){
                cerr<<"Error; return code from thread rdwrif1 create call is"<<thrd_rdwr<<endl;
                exit(-1);
            }

  pthread_attr_destroy(&attr_rdwr);

  
  //Read Write Thread End //


  thrd_sniffer=pthread_join(sniffer_thread,&status_sniffer);
            if(thrd_sniffer){
                cerr<<"Error; return code from thread sniffer1 join call is "<<thrd_sniffer<<endl;
                exit(-1);
            }

 thrd_rdwr=pthread_join(rdwr_thread,&status_rdwr);
            if(thrd_rdwr){
                cerr<<"Error; return code from thread rdwrif1 join call is "<<thrd_rdwr<<endl;
                exit(-1);
            }


return 0;

}